// pages/comic/comic.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bookmarkList:[],
    recommendComicList:[],
    latestComicList:[],
    total:0,
    avatar:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getData()
    
  },
  getData(){
    const userId = wx.getStorageSync('userId')
    app.$http.get('/app/bookmark/list', {pageNum:1,pageSize:3,mediaType:1,userId}).then(res =>{
      this.setData({
        total:res.data.total,
        bookmarkList:res.data.records
      })
      if(this.data.total == 0){
        app.$http.get('/app/user/'+wx.getStorageSync('userId')).then(res =>{
          res.data.avatar = app.$http.baseImgUrl + "/avatar/" + res.data.avatar
          this.setData({avatar:res.data.avatar})
        })
      }
      app.$http.get('/app/collection/list', {pageNum:1,pageSize:6,mediaType:1,curation:1}).then(res =>{
        this.setData({recommendComicList:res.data.records})
        app.$http.get('/app/collection/list', {pageNum:1,pageSize:6,mediaType:1,desc:'1'}).then(res =>{
          this.setData({latestComicList:res.data.records})
          //隐藏loading 提示框
          wx.hideLoading();
          //隐藏导航条加载动画
          wx.hideNavigationBarLoading();
          //停止下拉刷新
          wx.stopPullDownRefresh();
        })
      })
      
    }).catch(err => {
      console.log(err)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onRefresh()
  },
  //刷新
  onRefresh(){
    //在当前页面显示导航条加载动画
    wx.showNavigationBarLoading(); 
    //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
    wx.showLoading({
      title: '刷新中...',
    })
    this.getData();
  },
  toCollection: function(e){
    const collection = e.currentTarget.dataset.item;
    if(collection.collectionId){
      collection.id = collection.collectionId
    }
    wx.setStorageSync('collection', collection)
    wx.navigateTo({
      url: '/pages/index/collection/collection',
    })
  },
  onShow(){
    this.getData()
  },
  toMore: function(e){
    const id = e.currentTarget.dataset.id
    let url = ''
    switch (id){
      case '0':
        url = '/pages/mediaList/bookmark/bookmark?mediaType=1'
        break;
      case '1':
        url = '/pages/mediaList/mediaList?mediaType=1&type=1'
        break;
      case '2':
        url = '/pages/mediaList/mediaList?mediaType=1&type=2'
        break;
    }
    wx.navigateTo({url})
  },
})